package com.kob.backend.service.impl.user.bot;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backend.mapper.BotMapper;
import com.kob.backend.pojo.Bot;
import com.kob.backend.pojo.User;
import com.kob.backend.service.impl.UserDetailsImpl;
import com.kob.backend.service.user.bot.BotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BotServiceImpl implements BotService {

    @Autowired
    private BotMapper botMapper;

    @Override
    public Map<String, String> addBot(Bot bot) {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();
        bot.setUserId(user.getId());
        Map<String,String> map = new HashMap<>();
        String title = bot.getTitle();
        String content = bot.getContent();
        String description = bot.getDescription();
        if(title == null || title.length() == 0){
            map.put("message","标题不能为空");
            return map;
        }
        if(title.length() > 100){
            map.put("message","标题长度不能大于100");
            return map;
        }
        if(description == null || description.length() == 0){
            description = "这个人很懒，什么都没留下";
            bot.setDescription(description);
        }
        if(description.length() > 300){
            map.put("message","字数超过了哟,精简一点嘛！");
            return map;
        }
        if(content == null || content.length() == 0){
            map.put("message","bot代码不能为空！");
            return map;
        }
        if(content.length() > 10000){
            map.put("message","代码长度不能超过10000");
            return map;
        }
        Date date = new Date();
        bot.setCreateTime(date);
        bot.setModifyTime(date);
        botMapper.insert(bot);
        map.put("message","success");
        return map;
    }

    @Override
    public Map<String, String> deleteBot(Integer id) {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();
        Map<String,String> map = new HashMap<>();
        Bot bot = botMapper.selectById(id);
        if(bot == null){
            map.put("message","Bot不存在");
            return map;
        }
        if(!bot.getUserId().equals(user.getId())){
            map.put("message","没有权限删除该Bot");
            return map;
        }
        botMapper.deleteById(id);
        map.put("message","success");
        return map;
    }

    @Override
    public Map<String, String> updateBot(Map<String, String> data) {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();
        Map<String,String> map = new HashMap<>();
        int id = Integer.parseInt(data.get("id"));
        String title = data.get("title");
        String description = data.get("description");
        String content = data.get("content");
        if(title == null || title.length() == 0){
            map.put("message","标题不能为空");
            return map;
        }
        if(title.length() > 100){
            map.put("message","标题长度不能大于100");
            return map;
        }
        if(description == null || description.length() == 0){
            description = "这个人很懒，什么都没留下";
        }
        if(description.length() > 300){
            map.put("message","字数超过了哟,精简一点嘛！");
            return map;
        }
        if(content == null || content.length() == 0){
            map.put("message","bot代码不能为空！");
            return map;
        }
        if(content.length() > 10000){
            map.put("message","代码长度不能超过10000");
            return map;
        }
        Date date = new Date();
        Bot bot = botMapper.selectById(id);
        if(!bot.getUserId().equals(user.getId()) ){
            map.put("message","没有权限修改该bot");
            return map;
        }
        Bot new_bot = new Bot(id,user.getId(),title,description,content,bot.getCreateTime(),date);
        botMapper.updateById(new_bot);
        map.put("message","success");
        return map;
    }

    @Override
    public List<Bot> getBotsByUser() {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();
        QueryWrapper<Bot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getId());
        return botMapper.selectList(queryWrapper);
    }
}
